package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.extension.mapping.config.DeepConfig;
import com.github.yulichang.extension.mapping.relation.Relation;
import com.google.common.collect.Lists;
import com.linkhub.oms.common.enums.mp.LocalProductTypeEnum;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.dao.mapper.LocalProductEntityMapper;
import com.linkhub.oms.dao.mpservice.ILocalProductService;
import com.linkhub.oms.entity.LocalProductEntity;
import com.linkhub.oms.entity.OrderEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import com.linkhub.oms.component.mp.util.QtMpUpdateAssist;

/**
 * <p>
 * 本地商品表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-03-28
 */
@Service
public class LocalProductServiceImpl extends ServiceImpl<LocalProductEntityMapper, LocalProductEntity> implements ILocalProductService {

    @Override
    public LocalProductEntity getBySkuId( String skuId ) {
        return lambdaQuery()
                .eq(LocalProductEntity::getSkuId, skuId)
                .one();
    }

    @Override
    public void updateBySkuId( LocalProductEntity localProductEntity ) {
        QtMpUpdateAssist.updateByIdsWithSetNull(localProductEntity, this, LocalProductEntity::getUserId, LocalProductEntity::getProductType, LocalProductEntity::getSkuId);
    }

    @Override
    public LocalProductEntity getLocalProductBySku( String skuId, String userId, String sku ) {
        return lambdaQuery()
                .ne(StringUtils.isNotBlank(skuId), LocalProductEntity::getSkuId, skuId)
                .eq(LocalProductEntity::getUserId, userId)
                .eq(LocalProductEntity::getSku, sku)
                .last("LIMIT 1")
                .one();
    }

    @Override
    public LocalProductEntity getLocalProductByUpc( String skuId, String userId, String upc ) {
        return lambdaQuery()
                .ne(StringUtils.isNotBlank(skuId), LocalProductEntity::getSkuId, skuId)
                .eq(LocalProductEntity::getUserId, userId)
                .eq(LocalProductEntity::getUpc, upc)
                .last("LIMIT 1")
                .one();
    }

    @Override
    public LocalProductEntity getLocalProductByFnsku( String skuId, String userId, String fnsku ) {
        return lambdaQuery()
                .ne(StringUtils.isNotBlank(skuId), LocalProductEntity::getSkuId, skuId)
                .eq(LocalProductEntity::getUserId, userId)
                .eq(LocalProductEntity::getFnsku, fnsku)
                .last("LIMIT 1")
                .one();
    }

    @Override
    public LocalProductEntity getLocalProductByOtherCode( String skuId, String userId, String otherCode ) {
        return lambdaQuery()
                .ne(StringUtils.isNotBlank(skuId), LocalProductEntity::getSkuId, skuId)
                .eq(LocalProductEntity::getUserId, userId)
                .eq(LocalProductEntity::getOtherCode, otherCode)
                .last("LIMIT 1")
                .one();
    }

    @Override
    public List<String> listLocalProductCodes( String userId,
                                               Collection<String> codes,
                                               SFunction<LocalProductEntity, String> column ) {

        if (CollUtil.isEmpty(codes)) {
            return Lists.newArrayListWithCapacity(0);
        }

        return QtStreamUtil.map(lambdaQuery()
                        .select(LocalProductEntity::getId, column)
                        .eq(LocalProductEntity::getUserId, userId)
                        .in(column, codes)
                        .list(),
                column);
    }

    @Override
    public List<LocalProductEntity> listBySkuIds(Collection<String> skuIds) {
        if (CollUtil.isEmpty(skuIds)) {
            return Lists.newArrayListWithCapacity(0);
        }

        return lambdaQuery()
                .in(LocalProductEntity::getSkuId, skuIds)
                .list();
    }

    @Override
    public void updateLocalProductBySkuId(LocalProductEntity localProductEntity) {
        QtMpUpdateAssist.updateByIdsWithSetNull(localProductEntity, this, LocalProductEntity::getSkuId,LocalProductEntity::getUserId);

    }

    @Override
    public LocalProductEntity deepMappingLocalProduct( LocalProductEntity localProductEntity ) {
        if (Objects.isNull(localProductEntity)) {
            return null;
        }
        List<LocalProductEntity> mappedEntities =  Relation.mpjGetRelation(Lists.newArrayList(localProductEntity), DeepConfig.defaultConfig());
        // 如果返回的列表不为空，返回第一个元素，否则返回 null
        return mappedEntities == null || mappedEntities.isEmpty() ? null : mappedEntities.get(0);
    }

    @Override
    public List<LocalProductEntity> getLocalProductByType( LocalProductTypeEnum localProductTypeEnum ) {
        return lambdaQuery()
                .in(LocalProductEntity::getProductType, localProductTypeEnum)
                .orderByDesc(LocalProductEntity::getUpdateTime)
                .list();
    }
}
